﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using ZhouYu.NewWorld.BLL.IService;
using ZhouYu.NewWorld.Model;
using ZhouYu.NewWorld.ViewModel;
using ZhouYu.NewWorld.Web.Models;

namespace ZhouYu.NewWorld.Web.Controllers
{
    public class PictureController : Controller
    {
        //注入
        private IPictureService _pictureService;
        public PictureController(IPictureService pictureService)
        {
            _pictureService = pictureService;
        }

        [HttpPost]
        public JsonResult UploadImage(HttpPostedFileBase file)
        {
            //先定义文件
            string fileName = Guid.NewGuid().ToString("N");
            //图片存放的路径
            string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Resource/Uploads");
            //如果没有图片
            if (Request.Files.Count == 0)
            {
                return Json(new { Success = false, FilePath = "" });
            }
            //获取图片的扩展名
            string stuf = Path.GetExtension(file.FileName);

            //图片存放路径不存在则创建
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            //保存图片
            string path = Path.Combine(filePath, fileName + stuf);
            file.SaveAs(path);

            //返回前端
            return Json(new { Success = true, FilePath = "/Resource/Uploads/" + fileName + stuf });
        }

        /// <summary>
        /// 获取图片列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetPictures(int page, int limit)
        {
            //定义表数据总数变量
            int count = 0;
            Expression<Func<Picture, bool>> lambdaCategory = a => true;
            Expression<Func<Picture, int>> orderCategory = a => a.PictureId;
            //创建List集合接收业务层返回值
            List<Picture> pics = _pictureService.QueryPageList(page, limit, lambdaCategory, orderCategory, out count);
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            //框架属性赋值
            responseResult.code = 0;
            responseResult.count = count;
            responseResult.msg = "";
            responseResult.data = pics;
            //返回PageListResult对象
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult AddPicture(Picture pic)
        {
            pic.UpdateTime = DateTime.Now;
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            responseResult.Success = _pictureService.Add(pic);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult DeletePicture(Picture pic)
        {
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            responseResult.Success = _pictureService.Delete(pic);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 编辑图片
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult EditPicture(Picture pic)
        {
            pic.UpdateTime = DateTime.Now;
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            responseResult.Success = _pictureService.Update(pic);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 修改是否主图
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult UpdateMaster(int id, short states)
        {
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            //查询获取当前操作图片对象
            Expression<Func<Picture, bool>> lambdaCategory = a => a.PictureId == id;
            Picture pic = _pictureService.Query(lambdaCategory);
            //获取当前商品下的其他图片对象
            Expression<Func<Picture, bool>> lambdaCategory1 = a => a.GoodsId==pic.GoodsId&&a.PictureId!=id;
            List<Picture> pics = _pictureService.QueryList(lambdaCategory1);
            if (states != 0)
            {
                for (int i = 0; i < pics.Count; i++)
                {
                    pics[i].Is_Master = 0;
                    pics[i].UpdateTime = DateTime.Now;
                    responseResult.Success = _pictureService.Update(pic);
                    if (!responseResult.Success)
                    {
                        return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
                    }
                }
            }
            pic.Is_Master = states;
            pic.UpdateTime = DateTime.Now;
            responseResult.Success = _pictureService.Update(pic);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 修改是否有效
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult UpdatePicState(int id, short states)
        {
            Expression<Func<Picture, bool>> lambdaCategory = a => a.PictureId == id;
            Picture pic = _pictureService.Query(lambdaCategory);
            pic.Pic_Status = states;
            pic.UpdateTime = DateTime.Now;
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            responseResult.Success = _pictureService.Update(pic);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }


        /// <summary>
        /// 检查是否存在主图
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult CheckIsMaster(int goodsId)
        {
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            //获取当前商品下的图片集合
            Expression<Func<Picture, bool>> lambdaCategory = a => a.GoodsId == goodsId;
            List<Picture> pic = _pictureService.QueryList(lambdaCategory);
            //遍历集合判断是否存在主图
            for (int i = 0; i < pic.Count; i++)
            {
                if (pic[i].Is_Master == 1) 
                {
                    responseResult.msg = "添加失败！当前商品下已存在主图，无法继续设置主图";
                    responseResult.Success = false;
                    return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
                }
            }
            responseResult.Success = true;
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }
        
    }
}